package com.fk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fk.reggie.common.BaseContext;
import com.fk.reggie.common.R;
import com.fk.reggie.domain.ShoppingCart;
import com.fk.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    //请求 URL: http://localhost:8080/shoppingCart/add
    //请求方法: POST
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        ShoppingCart one = shoppingCartService.saveShoppingCart(shoppingCart);
//        //为当前购物车商品设置所属用户
//        Long userId = BaseContext.getThreadLocal();
//        shoppingCart.setUserId(userId);
//        shoppingCart.setCreateTime(LocalDateTime.now());
//        //查询该用户购物车下是否已经存在该商品
//        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
//        if (shoppingCart.getDishId() != null) {
//            //1.用户加入到购物车的是菜品   按照菜品id加用户名查询
//            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
//        } else if (shoppingCart.getSetmealId() != null) {
//            //2.用户加入到购物车的是套餐  按照套餐id加用户名查询
//            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        }
//        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
//        if (one != null) {
//            //该用户已经添加过   更新数量
//            one.setNumber(one.getNumber() + 1);
//            shoppingCartService.updateById(one);
//        } else {
//            //该用户没添加过   添加购物车商品
//            shoppingCart.setNumber(1);
//            shoppingCartService.save(shoppingCart);
//            one = shoppingCart;
//        }
        return R.success(one);
    }

    //请求 URL: http://localhost:8080/shoppingCart/list
    //请求方法: GET
    //查询当前用户购物车信息
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long userId = BaseContext.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                .orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(shoppingCartList);
    }

    //请求 URL: http://localhost:8080/shoppingCart/clean
    //请求方法: DELETE
    //清空购物车信息
    @DeleteMapping("/clean")
    public R<String> clean() {
        Long userId = BaseContext.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("清空成功");
    }

    //请求 URL: http://localhost:8080/shoppingCart/sub
    //请求方法: POST
    @PostMapping("/sub")
    //{dishId: "1397851668262465537", setmealId: null}
    //dishId
    //:
    //"1397851668262465537"
    //setmealId
    //:
    //null
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.subShoppingCart(shoppingCart);
//        Long userId = BaseContext.getThreadLocal();
//        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
//        shoppingCartUpdateWrapper.eq("user_id", userId);
//        if (shoppingCart.getSetmealId() != null) {
//            shoppingCartUpdateWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
//            //查询当前用户当前套餐的数量
//            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
//                    .eq(ShoppingCart::getUserId, userId);
//            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
//            Integer number = one.getNumber();
//
//            if (number==1){
//                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
//                        .eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//                shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
//                return R.success("修改成功");
//            }
//
//            shoppingCartUpdateWrapper.set("number", number - 1);
//
//
//            shoppingCartService.update(shoppingCartUpdateWrapper);
//        } else if (shoppingCart.getDishId() != null) {
//            shoppingCartUpdateWrapper.eq("dish_id", shoppingCart.getDishId());
//            //查询当前用户当前套餐的数量
//            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(ShoppingCart::getUserId, userId)
//                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId());
//            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
//            Integer number = one.getNumber();
//
//            //当前购物车数量为1
//            if (number==1){
//                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
//                                .eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//                shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
//                return R.success("修改成功");
//            }
//
//            shoppingCartUpdateWrapper.set("number", number - 1);
//            shoppingCartService.update(shoppingCartUpdateWrapper);
//        }
        return R.success("修改成功");
    }
}
